package com.adrian.controller;

import com.adrian.api.ApiClient;
import com.adrian.pojo.User;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: Adrian
 * @Date: 2020/3/19 21:12
 * @Description:
 */
@Controller
@Scope("prototype")
@Api(value = "用户的控制器",tags = {"用户的控制器"})
public class UserController {

    @Autowired
    private ApiClient apiClient;

    /**
     * 进入login页面
     *
     * @return
     */
    @RequestMapping({"/","login"})
    public String loginUI(){
        //跳转登录页面
        return "login";
    }

    @RequestMapping(value = "user_login",method = RequestMethod.POST)
    public void toLogin(User user, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        User user1 = apiClient.userLogin(user);
        System.out.println(user1);
        if (user1 != null) {
            response.getWriter().write("<script>alert('登录成功!');location.href='/product_list';</script>");
            System.out.println("登录成功templates");
        } else {
            response.getWriter().write("<script>alert('账号或密码错误!');location.href='/login';</script>");
            System.out.println("登录失败");
        }
    }
}
